package com.cuit.jifver.weibo.logic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.cuit.jifver.weibo.activity.BaseActivity;
import com.cuit.jifver.weibo.service.Service;
import com.cuit.jifver.weibo.utils.Constants;
import com.cuit.jifver.weibo.utils.LogUtil;



public class MainService {

	public static boolean isrun = false;
	public static MainService mainService;// global service
	// Save All Activity
	public static ArrayList<BaseActivity> allActivity = new ArrayList<BaseActivity>();


	
	
	
	public MainService()
	{
		mainService = this;
	}


	public static BaseActivity getActivityByName(String name)
	{
		BaseActivity baseac = null;
		for (BaseActivity ba : allActivity)
		{
			if (ba.getClass().getName().indexOf(name) >= 0)
			{
				baseac = ba;
			}
		}
		return baseac;
	}


	// Done Task
	public void doTask(Task task) {
		if (task == null)
			return;
		Map<String, Object> param = task.getTaskParam();
		//List list = new ArrayList<Object>();
		Message message = handler.obtainMessage();
		
		int taskid =  task.getTaskID();
		SavedLastRequestTask.savedLastTaskParams(taskid, param);
		
			switch (taskid) 
			{						
			case Constants.task.GET_RUEST_TOKEN:
				message.obj = Service.getRuestToken();
				LogUtil.i(MainService.class, "mssage"+message.obj);
				break;
			case Constants.task.GET_AUTHORIZATION_TOKEN:
				//Service.
				break;
			case Constants.task.GET_ACCESS_TOKEN:
				String verifier = (String) param.get(Constants.oauth.OAUTH_VERIFIER);
				message.obj = Service.getAccessToken(verifier);
				break;
			case Constants.task.GET_SINA_HOME_TIMELINE:
				Service.getSinaHomeTimeline();
				break;
			}
			message.what = task.getTaskID();
		
		// allTask.remove(task);// complete task, remove this task

		// Send refresh message to UI Thread
		handler.sendMessage(message);
	}

	// Refresh UI
		public Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				BaseActivity ac = null;
				String activityName = "";
				
				switch (msg.what) 
				{
				case Constants.task.GET_RUEST_TOKEN:
					//String result = (String) msg.obj;
					//LogUtil.i(this, "result="+result);
					activityName = "AuthorizationAct";
						break;
				case Constants.task.GET_ACCESS_TOKEN:
					activityName = "ShowAccessTokenAct";
					break;
					
				//case Config.TASK_GET_ALL_NAVIGATION:
				/*case Config.TASK_GET_SUB_NAVIGATION:
					ac = MainService
							.getActivityByName("ShopPageActivity");
					ac.refresh(msg.what, msg.obj);
					break;*/
				}
				
				//MainService.allActivity.remove(ac);
				ac = MainService.getActivityByName(activityName);
				//LogUtil.i(MainService.class, "msg"+msg.getData().get("oauth_token"));
				ac.refresh(msg.what, msg.obj);
			}
		};

	// Exit Application
	public static void exitApp(Activity context) {
		for (int i = 0; i < allActivity.size(); i++) {
			((Activity) allActivity.get(i)).finish();
		}
		allActivity.clear();
		System.exit(0);

	}

	// Add Task
	public static void addNewTask(final Task ts) {
		// allTask.add(ts);
		new Thread() {
			public void run() {
				mainService.doTask(ts);
			}
		}.start();
	}

	public static void promptExitApp(final Activity context) {
		// Create Dialog
		OnClickListener leftListener = new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				exitApp(context);// Exit All Application
			}
		};
		//DialogUtil.TwoButtonShowMessageDialog(context, R.string.exitApp, leftListener, null);
	}
}
